# Maintainer: David Runge <dvzrv@archlinux.org>

pkgbase=fltk
pkgname=(fltk fltk-docs)
pkgver=1.3.8
pkgrel=2
pkgdesc="Graphical user interface toolkit for X"
arch=(loong64 x86_64)
license=(custom:FLTK)
url="https://www.fltk.org/"
makedepends=(
  alsa-lib
  cmake
  doxygen
  fontconfig
  libglvnd
  libjpeg-turbo
  libpng
  libx11
  libxcursor
  libxfixes
  libxft
  libxinerama
  libxrender
  mesa
)
source=(https://github.com/$pkgbase/$pkgbase/archive/release-$pkgver/$pkgbase-release-$pkgver.tar.gz)
sha512sums=('197848d3b80a65cca936daf4f0b74609f0fe8332a4cd11af53385fb2aa45ad698b1e239a48732b118cd3cb189bc531711b72fb2eeeb85be887dc6c5a558fa4b3')
b2sums=('d0a963a38b62ea8cf1d5fe57fab6664fc64083ff41f17847d737f71bfff08578c682deefb9847a9ab5b93210fce2afbfc2020bdfcde714947beac9b3f7c84358')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_BUILD_TYPE=None
    -D FLTK_LIBDIR=/usr/lib
    -D OPTION_BUILD_SHARED_LIBS=ON
    -D OPTION_BUILD_HTML_DOCUMENTATION=ON
    -D OPTION_INSTALL_HTML_DOCUMENTATION=ON
    -S $pkgbase-release-$pkgver
    -W no-dev
  )

  # fat-lto-objects is required for non-mangled .a files in fltk
  CFLAGS+=" -ffat-lto-objects"
  CXXFLAGS+=" -ffat-lto-objects"

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
  # build documentation explicitly as it is otherwise not generated/ installed
  make VERBOSE=1 -C build/documentation html
}

check() {
  ctest --test-dir build --output-on-failure
}

package_fltk() {
  depends=(
    gcc-libs
    glibc
    hicolor-icon-theme
    fontconfig libfontconfig.so
    libglvnd
    libjpeg-turbo
    libpng
    libx11
    libxcursor
    libxext
    libxfixes
    libxft
    libxinerama
    libxrender
    sh
  )
  provides=(
    libfltk_images.so
    libfltk_gl.so
    libfltk_forms.so
    libfltk.so
  )
  options=(
    !docs
    staticlibs
  )

  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 $pkgbase-release-$pkgver/COPYING -t "$pkgdir/usr/share/licenses/$pkgname/"

  # remove man pages of examples
  rm -rvf "$pkgdir/usr/share/man/man6"

  (
    cd "$pkgdir"
    _pick $pkgbase-docs usr/share/doc/$pkgname/html
  )
}

package_fltk-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-release-$pkgver/COPYING -t "$pkgdir/usr/share/licenses/$pkgname/"
}
